import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes, CategoryItem, subCategoryItem, TopCategory } from '@/types'
import { topCategory } from '../constants'

const useTopCategory = topCategory.map(item => ({name:item}))

export default defineStore ('category',{
  state() {
      return {
        list: useTopCategory as CategoryItem[],
        topCategory: {} as TopCategory,
        subCategory: {} as subCategoryItem
      }
  },
  actions:{
    async getAllList(){
      const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
      res.data.result.forEach(item => item.open = false)
      // console.log(res.data);
      
      this.list = res.data.result
    },
    show(id: string){
      const item = this.list.find(item => item.id === id)
      if(item){
        item.open = true
      }

      //  item && (item.open = true)
    },
    hide(id: string){
      const item = this.list.find(item => item.id === id)
      if(item){
        item.open = false
      }
    },
    async getTopCategory(id: string){
      const res = await request.get('/category', {
        params: {
          id
        }
      })
      // console.log(res)
      this.topCategory = res.data.result
      
    },
    async getSubCategory(id: string){
      const res = await request.get<ApiRes<subCategoryItem>>('/category/sub/filter', {
        params:{
          id
        }
      })
      // console.log(res)
      this.subCategory = res.data.result
    }
  },
  getters:{}
})